package com.zdsoft.blp.zongbuju.shouye.wuye.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.TextView;

import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.bianmin.entity.SheQu;
import com.zdsoft.blp.zongbuju.shouye.wuye.adapter.TongZhiAdapter;
import com.zdsoft.blp.zongbuju.shouye.wuye.entity.XiangQing;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/8/30.
 */
public class WYTongZhiActivity extends BaseActivity implements NetCallback,XListView.IXListViewListener{
    private FanHuiView back;
    private XListView xlv;
    private List<XiangQing>list;
    private List<XiangQing>mlist;
    private XiangQing xiangQing;
    private int pageNo=1;
    private TongZhiAdapter adapter;
    @Override
    protected void init() {
        setContentView(R.layout.wy_tongzhigonggao);
        back=find(R.id.back);
        xlv=find(R.id.xlv);
        back.getTv_title().setText("通知公告");
        mlist=new ArrayList<>();
        xlv.setXListViewListener(this);
        xlv.setPullRefreshEnable(true);//设置可以刷新
        xlv.setPullLoadEnable(true);//设置可以加载更多
        showMoreData();

        xlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(WYTongZhiActivity.this,WYTongZhiXiangQing.class);
                intent.putExtra("id",mlist.get(position-1).getId());
                startActivity(intent);
            }
        });
    }

    private void showMoreData() {
        Net net=new Net();
        net.url= API.GENERALINFO_URL;
        net.nameSpace=API.GENERALINFO_NAMESPACE;
        net.methodName=API.GENERALINFOLIST ;
        SharedPreferences sp = getSharedPreferences("info", Context.MODE_PRIVATE);
        String communityCode = sp.getString("communityCode", "00001");
        Map<String,Object>map=new HashMap<>();
        map.put("pageNo",pageNo);
        map.put("pageSize",6);
        map.put("columnCode","tzgg");
        map.put("communityCode",communityCode);
        map.put("condition","");
        map.put("curTime","");
        net.parms=map;
        getServer(net,this);
    }

    @Override
    public void onResponse(boolean flag, int code, String res) {
       if (flag){
           if (pageNo==1){
               mlist=jiexi(res);
               adapter=new TongZhiAdapter(getApplicationContext(),mlist);
               xlv.setAdapter(adapter);
               xlv.stopRefresh();
           }else {
               list=jiexi(res);
               for (XiangQing xiangQings:list){
                   mlist.add(xiangQings);
               }
               adapter.setListData(mlist);
               xlv.stopLoadMore();
           }

       }
    }
    private List<XiangQing> jiexi(String res) {
        list = new ArrayList<>();
        try {
            JSONObject object=new JSONObject(res);
            JSONArray array=object.getJSONArray("rows");
            for(int i=0;i<array.length();i++){
                JSONObject object2=array.getJSONObject(i);
                String id=object2.getString("id");
                String picPath=object2.getString("picPath");
                String content=object2.getString("content");
                String title=object2.getString("title");
                String time=object2.getString("releaseDate");
                xiangQing=new XiangQing(id,title,time,content,picPath);
                list.add(xiangQing);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public void onRefresh() {
        mlist=new ArrayList<>();
        pageNo=1;
        showMoreData();
    }

    @Override
    public void onLoadMore() {
        pageNo++;
        showMoreData();
    }
}
